package net.qhzw.geologr5.gis.service.system.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.service.system.controller.ProjectPointType;
import net.qhzw.geologr5.gis.service.system.entity.ProjectPoint;
import net.qhzw.geologr5.gis.service.system.entity.ProjectType;
import net.qhzw.geologr5.gis.service.system.mapper.ProjectPointTypeMapper;
import net.qhzw.geologr5.gis.service.system.service.ProjectPointTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
@Slf4j
public class ProjectPointTypeServiceImpl implements ProjectPointTypeService {


    @Autowired
    private ProjectPointTypeMapper projectPointTypeMapper;


    /****
     * 查询点位类型
     * @return
     */
    @Override
    public JsonResult getProjectPointType() {
        try {
            List<ProjectPointType> projectPointTypes = projectPointTypeMapper.selectList(null);
            List<ProjectPointType> rootNode = new ArrayList<>();
            for (ProjectPointType projectPointType : projectPointTypes) {
                if (projectPointType.getPid() == 0) {
                    rootNode.add(projectPointType);
                }
            }
            for (ProjectPointType projectPointType : rootNode) {
                List<ProjectPointType> childrenList = geChildren(projectPointType.getId(), projectPointTypes);
                projectPointType.setProjectPointTypeList(childrenList);
            }
            return JsonResult.success(rootNode);
        } catch (Exception e) {
            log.error("查询点位类型失败==ProjectPointTypeServiceImpl==method:getProjectPointType==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }


    /***
     * 新增编辑 点位类型
     * @param projectPointType
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult edit(ProjectPointType projectPointType) {
        log.info("新增编辑类型==ProjectPointTypeServiceImpl==method:edit==param: {}", JSON.toJSONString(projectPointType));
        try {
            //修改操作
            if (projectPointType.getId() != null && projectPointType.getId() > 0) {
                if (projectPointType.getId().equals(projectPointType.getPid())) {
                    return JsonResult.error("修改数据不合法");
                }
                int row = projectPointTypeMapper.updateById(projectPointType);
                if (row > 0) {
                    return JsonResult.success("修改成功");
                } else {
                    return JsonResult.error("修改失败");
                }
            }
            int row = projectPointTypeMapper.insert(projectPointType);
            if (row > 0) {
                return JsonResult.success("添加成功");
            } else {
                return JsonResult.error("添加失败");
            }
        } catch (Exception e) {
            log.error("编辑点位类型失败==ProjectPointTypeServiceImpl==method:edit==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }

    /***
     * 删除点位类型
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult delete(Integer id) {
        try {
            ProjectPointType projectPoint = projectPointTypeMapper.selectProjectPointTypeById(id);
            if (projectPoint == null) {
                return JsonResult.error("暂无数据");
            }
            if (0 == projectPoint.getPid()) {
                List<ProjectPointType> projectPointTypeList = projectPointTypeMapper.selectProjectPointTypeByPid(projectPoint.getId());
                List<Integer> idList = new ArrayList<>();
                for (ProjectPointType projectPointType : projectPointTypeList) {
                    idList.add(projectPointType.getId());
                }
                idList.add(projectPoint.getId());
                int row = projectPointTypeMapper.deleteBatchIds(idList);
                if (row > 0) {
                    return JsonResult.success();
                } else {
                    return JsonResult.error("删除失败");
                }
            } else {
                int row = projectPointTypeMapper.deleteById(projectPoint.getId());
                if (row > 0) {
                    return JsonResult.success();
                } else {
                    return JsonResult.error("删除失败");
                }
            }
        } catch (Exception e) {
            log.error("删除点位类型失败==ProjectPointTypeServiceImpl==method:delete==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 获取子节点
     * @param id
     * @param projectPointTypes
     * @return
     */
    private List<ProjectPointType> geChildren(Integer id, List<ProjectPointType> projectPointTypes) {
        List<ProjectPointType> childList = new ArrayList<>();
        for (ProjectPointType projectPointType : projectPointTypes) {
            if (id.equals(projectPointType.getPid())) {
                childList.add(projectPointType);
            }
        }
        for (ProjectPointType projectPointType : childList) {
            List<ProjectPointType> child = geChildren(projectPointType.getId(), projectPointTypes);
            projectPointType.setProjectPointTypeList(child);
        }
        if (childList.size() == 0) {
            return new ArrayList<ProjectPointType>();
        }
        return childList;
    }
}
